/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.guix;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

/**
 *
 * @author DK
 */
public class DecisionRuleList extends JList {
//    private MainFrame mf;
//    private Controller ctrl;

//    private JLabel sourceLabel;
    private DefaultListModel uriListModel;
    
    private GDSearchingPanel gdsp;
    
    public DecisionRuleList (){
        
    }

    public DecisionRuleList(GDSearchingPanel gdsp) {
        this.gdsp = gdsp;
        this.uriListModel = new DefaultListModel();
        this.setModel(uriListModel);
        
        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.setBorder(BorderFactory.createTitledBorder("Select Rule"));
        
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 2) {
                    String select = getSelectedResource().toString();
                    int index = getSelectedResourcesIndex();
                    enterClickedRule(select, index);
                }
            }
        });


    }

    private void enterClickedRule(String result, int index) {
        // 룰 표현 패널에 넣기
        //해당하는 룰로 인스턴스 탐색
        this.gdsp.inputSelectRuleToTextField(result, index);
        
    }
    
    private int getSelectedResourcesIndex(){
        if(!this.isSelectionEmpty()){
            return this.getSelectedIndex();
        }
        return 0;
    }

    private String getSelectedResource() {
//        String s = new String();

        if (!this.isSelectionEmpty()) {
            Object select = this.getSelectedValue().toString();
            String s = select.toString();
            return s;
        }

        return null;
    }

    private void addURISetToList(Vector<String> rset) {
        for (int i = 0; i < rset.size(); i++) {
            this.uriListModel.addElement(rset.get(i));
        }
    }
    
    public void insertDecisionRuleToList(String rule, int index){        
        this.uriListModel.insertElementAt(rule,index);
    }
    
    public void clear(){
        this.uriListModel.clear();
    }
}
